/**
 * SpringMVC restful 风格
 * 共用控制器，控制关于共用的一些业务逻辑操作
 * @author yanwl
 * @date 2014-09-15
 */
package com.chnbst.bstyun.controller.user;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chnbst.bstyun.eos.Eos;
import com.chnbst.bstyun.model.pojo.Area;
import com.chnbst.bstyun.model.pojo.City;
import com.chnbst.bstyun.model.pojo.Goods;
import com.chnbst.bstyun.model.pojo.GoodsRelation;
import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.model.pojo.UserMessage;
import com.chnbst.bstyun.model.vo.OrderVo;
import com.chnbst.bstyun.service.GoodsRelationService;
import com.chnbst.bstyun.service.GoodsService;
import com.chnbst.bstyun.service.OperateApplyService;
import com.chnbst.bstyun.service.OrderService;
import com.chnbst.bstyun.service.PlaceService;
import com.chnbst.bstyun.service.UserMessageService;
import com.chnbst.bstyun.service.UserService;
import com.mysql.jdbc.StringUtils;

@Controller
@RequestMapping("/common")
public class CommonController {
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private UserMessageService userMessageService;
	@Autowired
	private UserService userService;
	@Autowired
	private PlaceService placeService;
	@Autowired
	private OperateApplyService operateApplyService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private Eos eos;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private GoodsRelationService goodsRelationService;

	/**
	 * post请求 ajax 执行添加用户留言
	 * http://xxx.com/common/usermessage
	 * @return boolean
	 */
	@RequestMapping(value="/usermessage",method=RequestMethod.POST)
	@ResponseBody
	public boolean usermessage(UserMessage userMessage){
		boolean isSuccess = false;
		if(userMessage != null) {
			isSuccess = userMessageService.insertUserMessage(userMessage);
		}
		return isSuccess;
	}
	
	/**
	 * POST请求 检查用户是否登录
	 * @param request
	 * @return boolean
	 */
	@RequestMapping(value = "/checkLogin", method = RequestMethod.POST)
	@ResponseBody
	public boolean checkLogin(HttpServletRequest request) throws Exception {
		boolean isLogin = true;
		User user = (User)request.getSession().getAttribute("userInfo");
		if(user == null) {
			isLogin = false;
		}
		return isLogin;
	}
	
	
	@RequestMapping(value="/citys",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getCityByPcode(HttpServletRequest request) throws JsonProcessingException, IOException{
		String provinceCode = request.getParameter("provinceCode");
		List<City> cityList = null;
		if(!StringUtils.isEmptyOrWhitespaceOnly(provinceCode)){
			cityList = placeService.getCityByPcode(Integer.parseInt(provinceCode));
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cityList", cityList);
		return map;
	}
	
	@RequestMapping(value="/areas",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getAreaByCcode(HttpServletRequest request) throws JsonProcessingException, IOException{
		String cityCode = request.getParameter("cityCode");
		List<Area> areaList = null;
		if(!StringUtils.isEmptyOrWhitespaceOnly(cityCode)){
			areaList = placeService.getAreaByCcode(Integer.parseInt(cityCode));
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("areaList", areaList);
		return map;
	}
	
	@RequestMapping(value="/getNowDate",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getNowDate(HttpServletRequest request){
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("time", new Date());
		return map;
	}
	
	@RequestMapping(value="/delCloseOrder",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delCloseOrder(HttpServletRequest request) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderState", 2);
		map.put("sortField", "o.pay_time");
		map.put("sortOrder", "desc");
		List<OrderVo> orders = orderService.selectCloseOrderVo(map);
		
		for (OrderVo order : orders) {
			Integer userId = order.getUserId();
			User user = userService.selectUserInfoByUserId(userId);
			String domainStr = user.getCompanyDomainName();
			String proMealName = order.getGoodsName();
			Goods goods = goodsService.selectByGoodsId(order.getGoodsId());
			if("0".equals(goods.getGoodsRalateGoods())) {
				//调用退单接口,即删除全部protal用户
				eos.chargeBack(domainStr,proMealName,userId,2);
			}else {
				List<GoodsRelation> goodsRelations = goodsRelationService.selectByGoodsId(goods.getGoodsId());
				String appCode = goodsRelations.get(0).getProductNum();
				//调用删除使用某应用的用户接口
				eos.deleteUsers(domainStr,appCode, proMealName,userId);
			}
		}
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("msg","执行成功");
		return resultMap;
	}
}
